package com.itheima.blog.admin.controller;


import com.itheima.blog.admin.domain.po.TCategory;
import com.itheima.blog.admin.domain.po.TTag;
import com.itheima.blog.admin.dto.CategoryDto;
import com.itheima.blog.admin.service.ITCategoryService;
import com.itheima.blog.admin.vo.CategoryMenuVO;
import com.itheima.common.domain.BlogPageQuery;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 博客分类表 前端控制器
 * </p>
 *
 * @author hyf
 * @since 2023-07-12
 */


@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class TCategoryController {
    private final ITCategoryService categoryService;
    @GetMapping("/category/page")
    @ApiOperation("分类分页查询")
    public PageResDto queryPage( BlogPageQuery pageQuery){
        PageResDto pageResDto = categoryService.queryPage(pageQuery);
        if (pageResDto!=null){
            pageResDto.setCode(200);
            pageResDto.setMsg("查询成功");
        }
        return pageResDto;
    }

    @PostMapping("/category")
    @ApiOperation("新增分类")
    public R insertCategory(@RequestBody TCategory category){
        Boolean aBoolean = categoryService.insertCategory(category);
        R r = new R<>();
        if (aBoolean){
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }
    @DeleteMapping("/category")
    @ApiOperation("删除分类")
    public R deleteCategory(String[] id){
        ArrayList<Integer> ids = new ArrayList<>();
        for (String i : id) {
            ids.add(Integer.parseInt(i));
        }
        Boolean aBoolean = categoryService.deleteCategory(ids);
        R r = new R<>();
        if (aBoolean){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }
    @GetMapping("/category/{categoryId}")
    @ApiOperation("根据id查询分类")
    public R queryCategoryList(@PathVariable("categoryId")Integer categoryId){
        CategoryDto categoryDto = categoryService.queryCategoryList(categoryId);
        return R.ok(categoryDto);
    }
    @ApiOperation("编辑分类")
    @PutMapping("/category/{categoryId}")
    public R updateCategory(@PathVariable("categoryId")Integer categoryId,@RequestBody TCategory tCategory){
        categoryService.updateCategory(categoryId,tCategory);
        return R.ok();
    }
    @PutMapping("/category/totop")
    @ApiOperation("置顶")
    public R  categoryStatus(@RequestBody TCategory tCategory){
        categoryService.categoryStatus(tCategory);
        return R.ok();
    }
    @GetMapping("/category/getCategoryById")
    public TCategory getCategoryById( Long categoryId){
        return categoryService.getById(categoryId);
    }
}
